package com.tocici.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.tocici.domain.bo.UserBehaviorBo;
import com.tocici.domain.vo.ArtistVo;
import com.tocici.domain.vo.UserBehaviorVo;
import com.tocici.service.IArtistService;
import com.tocici.service.IUserBehaviorService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.tocici.domain.vo.MusicVo;
import com.tocici.domain.bo.MusicBo;
import com.tocici.service.IMusicService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 音乐
 *
 * @author zeratul
 * @date 2025-03-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/music/music")
public class MusicController extends BaseController {

    private final IMusicService musicService;

    private final IUserBehaviorService userBehaviorService;

    private final IArtistService artistService;

    /**
     * 查询音乐列表
     */
    @SaCheckPermission("music:music:list")
    @GetMapping("/list")
    public TableDataInfo<MusicVo> list(MusicBo bo, PageQuery pageQuery) {
        return musicService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出音乐列表
     */
    @SaCheckPermission("music:music:export")
    @Log(title = "音乐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MusicBo bo, HttpServletResponse response) {
        List<MusicVo> list = musicService.queryList(bo);
        ExcelUtil.exportExcel(list, "音乐", MusicVo.class, response);
    }

    /**
     * 获取音乐详细信息
     *
     * @param musicId 主键
     */
    @SaCheckPermission("music:music:query")
    @GetMapping("/{musicId}")
    public R<MusicVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long musicId) {
        return R.ok(musicService.queryById(musicId));
    }

    /**
     * 新增音乐
     */
    @SaCheckPermission("music:music:add")
    @Log(title = "音乐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MusicBo bo) {
        return toAjax(musicService.insertByBo(bo));
    }

    /**
     * 修改音乐
     */
    @SaCheckPermission("music:music:edit")
    @Log(title = "音乐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MusicBo bo) {
        return toAjax(musicService.updateByBo(bo));
    }

    /**
     * 删除音乐
     *
     * @param musicIds 主键串
     */
    @SaCheckPermission("music:music:remove")
    @Log(title = "音乐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{musicIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] musicIds) {
        return toAjax(musicService.deleteWithValidByIds(List.of(musicIds), true));
    }

    @GetMapping("/new")
    @SaIgnore
    public R<List<MusicVo>> getNewSongs(){
        List<MusicVo> newSongs = this.musicService.getNewSongs(10);
        return R.ok(newSongs);
    }

    @GetMapping("/hot")
    @SaIgnore
    public R<List<MusicVo>> getHotSongs(){
        List<MusicVo> musicVos = this.musicService.getHotSongs(4);
        return R.ok(musicVos);
    }

    @GetMapping("/likes/user/{userId}")
    @SaIgnore
    public R<List<MusicVo>> getLikesSongs(@PathVariable Long userId){
        UserBehaviorBo bo = new UserBehaviorBo();
        bo.setUserId(userId);
        bo.setIsLiked(1);
        List<UserBehaviorVo> userBehaviorVos = userBehaviorService.queryList(bo);
        userBehaviorVos.forEach(userBehaviorVo -> {
            Long musicId = userBehaviorVo.getMusicId();
            MusicVo musicVo = musicService.queryById(musicId);
            userBehaviorVo.setMusic(musicVo);
        });
        List<MusicVo> list = userBehaviorVos.stream().map(UserBehaviorVo::getMusic).toList();
        list.forEach(m -> {
            Long artistId = m.getArtistId();
            ArtistVo artistVo = artistService.queryById(artistId);
            m.setArtistVo(artistVo);
        });
        return R.ok(list);
    }
}
